// <![CDATA[
jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function ( oSettings, iDelay ) {
  var _that = this;
  this.each( function ( i ) {
    $.fn.dataTableExt.iApiIndex = i;
    var	iDelay = (iDelay && (/^[0-9]+$/.test(iDelay)) ? iDelay : 250),
    $this = this,  oTimerId = null, sPreviousSearch = null,
    anControl = $( 'div.dataTables_filter input:text' );
    anControl.unbind( 'keyup' ).bind( 'keyup', function() {
    var $$this = $this;
    if (sPreviousSearch === null || sPreviousSearch != anControl.val()) {
      window.clearTimeout(oTimerId);
      sPreviousSearch = anControl.val();
      oTimerId = window.setTimeout(function() {
        $.fn.dataTableExt.iApiIndex = i;
        _that.fnFilter( anControl.val() );
        }, iDelay);
      }
    });
    return this;
    } );
  return this;
};

dataTables = function(id, ajaxSource, aoColumns) {
      var oTable = $("#"+id).dataTable({
                  "bJQueryUI": true,
                  'bProcessing': true,
                  'bServerSide': true,
                  'bAutoWidth': false,
                  'bLengthChange': false,
                  "sPaginationType": "full_numbers",
                  'sAjaxSource': ajaxSource,
                  "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                    if (aData[0] == fnGetSelected(this))
                    {
                      $(nRow).addClass('row_selected');
                    }
                    return nRow;
                  },
                  "aoColumns": aoColumns
      }).fnSetFilteringDelay();
      return oTable;
};

dataTablesWithParam = function(id, ajaxSource, aoColumns) {
      var oTable = $("#"+id).dataTable({
                  "bJQueryUI": true,
                  'bProcessing': true,
                  'bServerSide': true,
                  'bAutoWidth': false,
                  'bLengthChange': false,
                  "sPaginationType": "full_numbers",
                  'sAjaxSource': ajaxSource,
                  "fnServerData": function ( sSource, aoData, fnCallback ) {
                    aoData.push( fnGetAdditionalParams() );
                    $.ajax( {
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": fnCallback
                    });
                  },
                  "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                    if (aData[0] == fnGetSelected(this))
                    {
                      $(nRow).addClass('row_selected');
                    }
                    return nRow;
                  },
                  "aoColumns": aoColumns
      }).fnSetFilteringDelay();
      return oTable;
};

fnGetSelected = function( oTableLocal )
{
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            aReturn.push( aTrs[i] );
        }
    }
    return aReturn;
};

fnDelete = function(oTable, delURL) {
     jConfirm('Do you want to delete the record?', 'Message', function(r) {
         if(r){
           $.ajax({
            url: delURL,
            type: 'POST',
            cache: false,
            success: function (html) {
                if (html.valid == 'true') {
                    var anSelected = fnGetSelected( oTable );
                    oTable.fnDeleteRow( anSelected[0] );
                    jAlert(html.message, 'Message');
                } else {
                   jAlert(html.message, 'Alert Message');
                }
              }
            }, 'json');
         }
     });
};
// ]]>